'use strict';


export default class extends think.Controller {
	
	//登陆
	async signinAction(){
		if(this.isPost){
			let username = this.post('username');
            let password = think.md5(this.post('password'));	
			
			//查询是否有用户名
			let userinfo = await this.model('admin').where({username:username}).find();
			
			if(think.isEmpty(userinfo)){
				return this.fail(1,'用户名不存在！');	
			}else if(password !== userinfo.password)
			{
				return this.fail(1,'密码错误！');
				
			}else
			{
				await this.session('userinfo',userinfo);
				return this.success('登陆成功啦！');
			}	
		}	
		return this.display();
	}
	
	//登出
	async logoutAction(){
		await this.session('userinfo', null);
        this.redirect('/admin/public/signin');
	}
	
	//获取七牛上传图片的验证token
	async gettokenAction(){
		
		let qiniu = require("qiniu");

		//需要填写你的 Access Key 和 Secret Key
		qiniu.conf.ACCESS_KEY = this.config('qiniu.ACCESS_KEY');
		qiniu.conf.SECRET_KEY = this.config('qiniu.SECRET_KEY');
		//要上传的空间
		let bucket = this.config('qiniu.BUCKET');
		
		//构建上传策略函数
		function uptoken(bucket, key) {
		  let putPolicy = new qiniu.rs.PutPolicy(bucket);
		  return putPolicy.token();
		}
		//生成上传 Token
		let token = uptoken(bucket);
		let data = {'uptoken':token};
		return this.json(data);	
	}
	
	
	//获取左侧的分类列表
	async getMenuAction(){
		let mlist = await this.model('menu').where({status:1,is_hidden:0}).select();
		return this.json(mlist);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}